//package com.github.aloxc.plugin.restplus.component;
//
//import com.github.aloxc.plugin.restplus.listener.SendActionListener;
//import com.intellij.execution.DefaultExecutionResult;
//import com.intellij.execution.ExecutionException;
//import com.intellij.execution.ExecutionManager;
//import com.intellij.execution.Executor;
//import com.intellij.execution.configurations.RunProfile;
//import com.intellij.execution.configurations.RunProfileState;
//import com.intellij.execution.filters.TextConsoleBuilder;
//import com.intellij.execution.filters.TextConsoleBuilderFactory;
//import com.intellij.execution.runners.ExecutionEnvironment;
//import com.intellij.execution.ui.ConsoleView;
//import com.intellij.execution.ui.ConsoleViewContentType;
//import com.intellij.execution.ui.RunContentDescriptor;
//import com.intellij.execution.ui.RunnerLayoutUi;
//import com.intellij.icons.AllIcons;
//import com.intellij.openapi.Disposable;
//import com.intellij.openapi.actionSystem.*;
//import com.intellij.openapi.application.ApplicationManager;
//import com.intellij.openapi.editor.markup.TextAttributes;
//import com.intellij.openapi.fileEditor.FileDocumentManager;
//import com.intellij.openapi.project.DumbAware;
//import com.intellij.openapi.project.Project;
//import com.intellij.openapi.util.Computable;
//import com.intellij.openapi.util.Disposer;
//import com.intellij.openapi.wm.ToolWindowManager;
//import com.intellij.ui.content.Content;
//import org.jetbrains.annotations.NotNull;
//import org.jetbrains.annotations.Nullable;
//
//import javax.swing.*;
//import java.awt.*;
//import java.util.ArrayList;
//
//public class RunConsoleExecutor implements Disposable {
//    private Runnable myRerunAction;
//    private Runnable myStopAction;
//    private Computable<Boolean> myStopEnabled;
//    private boolean myActivateToolWindow = true;
//    private Project project;
//    private ConsoleViewContentType type = new ConsoleViewContentType("a",new TextAttributes());
//
//    public RunConsoleExecutor(Project project){
//        this.project = project;
//    }
//    @Override
//    public void dispose() {
//        Disposer.dispose(this);
//    }
//
//    private ConsoleView createConsole(@NotNull Project project) {
//        TextConsoleBuilder consoleBuilder = TextConsoleBuilderFactory.getInstance().createBuilder(project);
//        consoleBuilder.filters(new ArrayList<>());
//        ConsoleView console = consoleBuilder.getConsole();
////        console.attachToProcess(processHandler);
//        return console;
//    }
//
//    private static JComponent createConsolePanel(ConsoleView view, ActionGroup actions) {
//        JPanel panel = new JPanel();
//        panel.setLayout(new BorderLayout());
//        panel.add(view.getComponent(), BorderLayout.CENTER);
////        panel.add(createToolbar(actions), BorderLayout.WEST);
//        return panel;
//    }
//    private ActionGroup createActionToolbar(JComponent consolePanel, ConsoleView consoleView, @NotNull final RunnerLayoutUi myUi, RunContentDescriptor contentDescriptor, Executor runExecutorInstance) {
//        final DefaultActionGroup actionGroup = new DefaultActionGroup();
//        actionGroup.add(new RerunAction(consolePanel, consoleView));
//        actionGroup.add(new StopAction());
//        actionGroup.add(consoleView.createConsoleActions()[2]);
//        actionGroup.add(consoleView.createConsoleActions()[3]);
//        actionGroup.add(consoleView.createConsoleActions()[5]);
//        return actionGroup;
//    }
//
//    private class StopAction extends AnAction implements DumbAware {
//        public StopAction() {
//            super("Stop", "Stop", AllIcons.Actions.Suspend);
//        }
//
//        @Override
//        public void actionPerformed(AnActionEvent e) {
//            myStopAction.run();
//        }
//
//        @Override
//        public void update(AnActionEvent e) {
//            e.getPresentation().setVisible(myStopAction != null);
//            e.getPresentation().setEnabled(myStopEnabled != null && myStopEnabled.compute());
//        }
//    }
//    private class RerunAction extends AnAction implements DumbAware {
//        private final ConsoleView consoleView;
//
//        public RerunAction(JComponent consolePanel, ConsoleView consoleView) {
//            super("Rerun", "Rerun", AllIcons.Actions.Restart);
//            this.consoleView = consoleView;
//            registerCustomShortcutSet(CommonShortcuts.getRerun(), consolePanel);
//        }
//
//        @Override
//        public void actionPerformed(AnActionEvent e) {
//            Disposer.dispose(consoleView);
//            myRerunAction.run();
//        }
//
//        @Override
//        public void update(AnActionEvent e) {
//            e.getPresentation().setVisible(myRerunAction != null);
//            e.getPresentation().setIcon(AllIcons.Actions.Restart);
//        }
//    }
//    public void activateToolWindow() {
//        ApplicationManager.getApplication().invokeLater(() -> ToolWindowManager.getInstance(project).getToolWindow(RunExecutor.TOOLWINDOWS_ID).activate(null));
//    }
//    public RunConsoleExecutor withActivateToolWindow(boolean activateToolWindow) {
//        myActivateToolWindow = activateToolWindow;
//        return this;
//    }
//    public ConsoleView run(Project project){
//        this.project = project;
//        FileDocumentManager.getInstance().saveAllDocuments();
//        final RunnerLayoutUi.Factory factory = RunnerLayoutUi.Factory.getInstance(this.project);
//        final RunnerLayoutUi layoutUi = factory.create("RestPlusConsole", "RestPlusConsole", "RestPlusConsole", this.project);
//        DefaultActionGroup actions = new DefaultActionGroup();
//        ConsoleView consoleView = createConsole(this.project);
//        final JComponent consolePanel = createConsolePanel(consoleView, actions);
//        RunContentDescriptor descriptor = new RunContentDescriptor(new RunProfile() {
//            @Nullable
//            @Override
//            public RunProfileState getState(@NotNull Executor executor, @NotNull ExecutionEnvironment executionEnvironment) throws ExecutionException {
//                return null;
//            }
//
//            @Override
//            public String getName() {
//                //第一层名称显示
//                return "RestPlus";
//            }
//
//            @Nullable
//            @Override
//            public Icon getIcon() {
//                return null;
//            }
//        }, new DefaultExecutionResult(), layoutUi);
//
//        descriptor.setExecutionId(System.nanoTime());
//        descriptor.setFocusComputable(() -> consoleView.getPreferredFocusableComponent());
//        descriptor.setAutoFocusContent(true);
//        descriptor.setContentToolWindowId(RunExecutor.TOOLWINDOWS_ID);
//
//        //第二层名称显示
//        final Content content = layoutUi.createContent("ConsoleContent", consolePanel, "RestPlus executable", AllIcons.Debugger.Console, consolePanel);
//        content.setCloseable(false);
//        layoutUi.addContent(content);
//        Executor executor = RunExecutor.getRunExecutorInstance();
//        layoutUi.getOptions().setLeftToolbar(createActionToolbar(consolePanel, consoleView, layoutUi, descriptor, executor), "RunnerToolbar");
//        consoleView.print("---"+project.getName()+"---",type);
//        Disposer.register(descriptor, this);
//        Disposer.register(content, consoleView);
//        for (AnAction action : consoleView.createConsoleActions()) {
//            actions.add(action);
//        }
//
//        ExecutionManager.getInstance(this.project).getContentManager().showRunContent(executor, descriptor);
//        if (myActivateToolWindow) {
//            activateToolWindow();
//        }
//        return consoleView;
//    }
//}
